<template>
  <div class="container">
    <el-dialog
      :visible.sync="value"
      width="40%"
      top="0"
      :before-close="handleClose"
      custom-class="dialogStyle"
    >
      <div slot="title" class="title_box">
        <h4>虚拟成员（{{ total || 0 }}）</h4>
        <div class="btn">
          <el-button type="primary" size="mini" @click="$emit('add')">新 增</el-button>
          <span>（点击新增添加虚拟成员）</span>
        </div>
      </div>

      <div class="content">
        <el-table
          v-loading="loading"
          :data="list"
          :header-cell-style="{'color':'#000'}"
          style="width: 100%"
        >
          <el-table-column
            label="头像"
            align="center"
          >
            <template slot-scope="scope">
              <div>
                <img :src="scope.row.avatar" alt="" style="width:50px;height:50px;object-fit:cover;">
              </div>
            </template>
          </el-table-column>
          <el-table-column
            label="昵称"
            align="center"
            prop="nickname"
          />

          <el-table-column
            label="创建时间"
            align="center"
            prop="created_at"
          />

          <el-table-column label="操作" fixed="right" align="center">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="del(scope.row.id)"><i class="el-icon-delete" style="margin-right: 4px;" />删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination">
          <el-pagination
            layout="prev, pager, next"
            :page-size="limit"
            :total="total"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: {
    pubVue: {
      type: Object,
      default: () => {
        return {}
      }
    },
    value: {
      type: Boolean,
      default: false
    },
    groupId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      loading: false,
      list: [],
      page: 1,
      limit: 10,
      total: 0
    }
  },
  watch: {
    groupId(e) {
      this.init()
    }
  },
  mounted() {
    this.pubVue.$on('refresh', () => {
      this.getMenber()
    })
  },
  methods: {
    // 关闭弹窗
    handleClose() {
      this.$emit('input', false)
    },

    init() {
      this.page = 1
      this.limit = 10
      this.total = 0
      this.list = []
      this.getMenber()
    },

    // 分页
    handleCurrentChange(e) {
      this.page = e
      this.getMenber()
    },

    // 获取群成员
    getMenber() {
      this.loading = true
      this.$request.post('/admin/group/robot/list', {
        group_id: this.groupId,
        limit: this.limit,
        page: this.page
      }).then(res => {
        this.loading = false
        if (res.code == 200) {
          this.list = res.data.data
          this.total = res.data.total
        }
      }).catch(() => { this.loading = false })
    },

    // 删除
    del(e) {
      this.$request.post(`/admin/group/robot/delete/${e}`).then(res => {
        if (res.code == 200) {
          this.getMenber()
        }
      }).catch(() => { })
    }
  }
}
</script>

<style scoped>
  .container /deep/ .dialogStyle{
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
  }
  .container /deep/ .el-dialog__body {
    padding: 20px 20px 0;
  }
</style>
<style lang="scss" scoped>
    .seach{
      width: 100%;
      height: auto;
      padding: 10px;
      background-color: #fff;
      display: flex;
      justify-content: space-between;

      &>div{
        width: calc(100% / 4 - 20px);
        display: flex;
        align-items: center;

        &:nth-child(3){
          width: calc(100% / 4 + 80px);
        }
        &:nth-child(4){
          width: calc(100% / 4 - 100px);
        }
        &>.title{
          display: inline-block;
          width: 110px;
          font-size: 14px;
        }
      }
    }

  .content{
    width: 100%;
  }
  .btn{
    &>span{
      display: inline-block;
      color: #1989fa;
      text-align: center;
      cursor: pointer;
    }
  }

  .look{
    width: 100%;
    font-size: 14px;
    color: #1989fa;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    &>span{ margin-left: 10px; }
  }

  .dialog-footer{
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    &>div{
      margin-left: 30px;
      &>span{
        font-size: 16px;
      }
    }
  }

  .pagination{
    width: 100%;
    height: 80px;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #fff;
  }

  .title_box{
    width: 100%;
    align-items: center;
    display: flex;
    &>h4{
      line-height: 24px;
    }
    &>.btn{
      margin-left: 20px;
      &>span{
        font-size: 14px;
      }
    }
  }
</style>
